<%@ page language="java" pageEncoding="UTF-8"
	contentType="text/html; charset=UTF-8"%>
<script type="text/javascript">
	$(function() {

		$('#alarm_server_datagrid').datagrid({
			url : '${pageContext.request.contextPath}/alarmServer/list',
			method : 'GET',
			fit : true,
			//fitColumns : true,
			border : false,
			pagination : true,
			idField : 'id',
			pageSize : 20,
			pageList : [20, 30, 40, 50 ],
			sortName : 'id',
			sortOrder : 'asc',
			checkOnSelect : false,
			selectOnCheck : false,
			frozenColumns : [ [ {
				field : 'id',
				title : '编号',
				width : 150,
				checkbox : true
			}, {
				field : 'serverName',
				title : '城市名称',
				align : 'center',
				width : 120,
				sortable : true
			},{
				field : 'url',
				title : '监控url',
				align : 'center',
				width : 300
			}, {
				field : 'status',
				title : '状态',
				align : 'center',
				width : 120,
				formatter : function(value, row, index) {
					if (value == 0) {
						return '<font color="#FF0000">服务异常</font>';
					} else {
						return '正常';
					}

				}

			}] ],
			columns : [ [  {
				field : 'flag',
				title : '是否开启',
				align : 'center',
				width : 60,
				formatter : function(value, row, index) {
					if (value == "on") {
						return '开启';
					} else {
						return '关闭';
					}

				}
			} , {
				field : 'method',
				title : '请求方式',
				align : 'center',
				width : 60,
				formatter : function(value, row, index) {
					if (value == 0) {
						return 'GET';
					} else if (value == 1) {
						return 'POST';
					} else if (value == 2) {
						return 'PUT';
					} else {
						return 'DELETE';
					}

				}

			}, {
				field : 'params',
				title : '参数',
				align : 'center',
				width : 120

			}, {
				field : 'minFailureTimes',
				title : '最小失败次数',
				align : 'center',
				width : 80
			} , {
				field : 'respResult',
				title : '包含结果',
				align : 'center',
				width : 120

			}, {
				field : 'notNullField',
				title : '非空字段',
				align : 'center',
				width : 120

			}, {
				field : 'noticePhones',
				title : '手机',
				align : 'center',
				width : 120

			}, {
				field : 'noticeWeixins',
				title : '微信',
				align : 'center',
				width : 120

			}, {
				field : 'noticeEmails',
				title : '邮箱',
				align : 'center',
				width : 120

			}, {
				field : 'noticeEmailSubject',
				title : '邮箱主题',
				align : 'center',
				width : 120

			} ,
			{
				field : 'action',
				title : '动作',
				width : 100,
				formatter : function(value, row, index) {
					return formatString(
								'<img onclick="alarm_server_editFun(\'{0}\');" src="{1}"/>&nbsp;<img onclick="alarm_server_deleteFun(\'{2}\');" src="{3}"/>&nbsp;',
								row.id,
								'${pageContext.request.contextPath}/style/images/extjs_icons/pencil.png',
								row.id,
								'${pageContext.request.contextPath}/style/images/extjs_icons/cancel.png'
								);
				
				}
			}] ],
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#alarm_server_menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			},
			toolbar : [ {
				text : '增加',
				iconCls : 'icon-add',
				handler : function() {
					alarm_server_appendFun();
				}
			}, '-', {
				text : '批量删除',
				iconCls : 'icon-remove',
				handler : function() {
					alarm_server_removeFun();
				}
			} ]
		});

	});

	function alarm_server_editFun(id) {
		/* $('#alarm_server_datagrid').datagrid('uncheckAll').datagrid(
				'unselectAll').datagrid('clearSelections'); */
		var p = parent.lw
				.dialog({
					href : '${pageContext.request.contextPath}/alarm/alarmServerEdit.jsp',
					width : 820,
					height : 320,
					modal : true,
					title : '编辑服务监控',
					buttons : [ {
						text : '编辑',
						iconCls : 'icon-edit',
						handler : function() {
							var d = $(this).closest('.window-body');
							$('#alarm_serverEdit_editForm')
									.form(
											'submit',
											{
												url : '${pageContext.request.contextPath}/alarmServer/updateByPrimaryKeySelective',
												success : function(result) {
													try {
														var r = $
																.parseJSON(result);
														if (r.success) {
															$(
																	'#alarm_server_datagrid')
																	.datagrid(
																			'reload');
															d.dialog('destroy');
														}
														$.messager.show({
															title : '提示',
															msg : r.msg
														});
														p.dialog('close');
													} catch (e) {

														$.messager.alert(
																'提示异常', e);
													}
												}
											});
						}
					} ],
					onClose : function() {
						$(this).dialog('destroy');
					},
					onLoad : function() {
						var index = $('#alarm_server_datagrid').datagrid(
								'getRowIndex', id);
						var rows = $('#alarm_server_datagrid').datagrid(
								'getRows');
						var o = rows[index];
						$('#alarm_serverEdit_editForm').form('load', o);
					}
				});
	}
	function alarm_server_appendFun() {
		$('#alarm_server_datagrid').datagrid('uncheckAll').datagrid(
				'unselectAll').datagrid('clearSelections');
		var p = parent.lw
				.dialog({
					href : '${pageContext.request.contextPath}/alarm/alarmServerAdd.jsp',
					width : 820,
					height : 320,
					modal : true,
					title : '添加服务监控',
					buttons : [ {
						text : '增加',
						iconCls : 'icon-add',
						handler : function() {
							var d = $(this).closest('.window-body');
							$('#alarm_serverAdd_addForm')
									.form(
											'submit',
											{
												url : '${pageContext.request.contextPath}/alarmServer/insertSelective',
												success : function(result) {
													try {
														var r = $
																.parseJSON(result);
														if (r.success) {
															$(
																	'#alarm_server_datagrid')
																	.datagrid(
																			'load');
															$(
																	'#alarm_server_datagrid')
																	.datagrid(
																			'uncheckAll')
																	.datagrid(
																			'unselectAll')
																	.datagrid(
																			'clearSelections');
															d.dialog('destroy');
														}
														$.messager.show({
															title : '提示',
															msg : r.msg
														});
														p.dialog('close');
													} catch (e) {
														$.messager.alert('提示',
																result);
													}
												}
											});
						}
					} ],
					onClose : function() {
						$(this).dialog('destroy');
					}
				});
	}
	function alarm_server_removeFun() {
		var rows = $('#alarm_server_datagrid').datagrid('getChecked');
		if (rows.length > 0) {
			$.messager
					.confirm(
							'确认',
							'您是否要删除当前选中的项目？',
							function(r) {
								if (r) {

									$.ajax({
												url : '${pageContext.request.contextPath}/alarmServer/deleteByPrimaryKey',
												data : {
													id : rows[0].id
												},
												dataType : 'json',
												success : function(result) {
													if (result.success) {
														$(
																'#alarm_server_datagrid')
																.datagrid(
																		'load');
														$(
																'#alarm_server_datagrid')
																.datagrid(
																		'uncheckAll')
																.datagrid(
																		'unselectAll')
																.datagrid(
																		'clearSelections');
													}
														$.messager.show({
															title : '提示',
															msg : result.msg
														});
												}
											});
								}
							});
		} else {
			$.messager.show({
				title : '提示',
				msg : '请勾选要删除的记录！'
			});
		}
	}
	function alarm_server_deleteFun(id) {
		$('#alarm_server_datagrid').datagrid('uncheckAll')
				.datagrid('unselectAll').datagrid('clearSelections');
		$('#alarm_server_datagrid').datagrid('checkRow',
				$('#alarm_server_datagrid').datagrid('getRowIndex', id));
		alarm_server_removeFun();
	}
</script>
<div class="easyui-layout" data-options="fit : true,border : false">
	<div data-options="region:'center',border:false">
		<table id="alarm_server_datagrid"></table>
	</div>
</div>

<div id="alarm_server_menu" class="easyui-menu"
	style="width: 120px; display: none;">
	<div onclick="alarm_server_appendFun();"
		data-options="iconCls:'icon-add'">增加</div>
	<div onclick="alarm_server_editFun();"
		data-options="iconCls:'icon-edit'">编辑</div>
</div>